<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- 组件不要提前渲染 bootstrap 会有bug -->

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="/shop-app/js/jquery.min.js"></script>
    <!--引入HttpUtil.js-->
    <script src="/shop-app/js/HttpUtil.js"></script>
    <!--引入存到浏览器的js文件-->
    <script src="/shop-app/js/UserCacheUtil.js"></script>
    <!-- 界面报icon找不到问题 -->
    <link rel="shortcut icon" href="#"/>
</head>
<body>

<nav class="navbar navbar-default alert-success">
    <div class="container">

        <!-- ********  这个是品牌图标  ********** -->

        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/shop-app/index.html">商城</a>
        </div>

        <!-- ********  这个是左边的链接  ********** -->

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
<!--                <li class="active "><a  href="#">帮助 <span class="sr-only">(current)</span></a></li>-->
                <li><a href="#">帮助</a></li>
                <li><a href="#">问题反馈</a></li>
                <!--                <li class="dropdown">-->
                <!--                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">下载 <span class="caret"></span></a>-->
                <!--                    <ul class="dropdown-menu">-->
                <!--                        <li><a href="#">Action</a></li>-->
                <!--                        <li><a href="#">Another action</a></li>-->
                <!--                        <li><a href="#">Something else here</a></li>-->
                <!--                        <li role="separator" class="divider"></li>-->
                <!--                        <li><a href="#">Separated link</a></li>-->
                <!--                        <li role="separator" class="divider"></li>-->
                <!--                        <li><a href="#">One more separated link</a></li>-->
                <!--                    </ul>-->
                <!--                </li>-->
            </ul>

            <!-- ********  这边是搜素条  ********** -->

            <!--            <form class="navbar-form navbar-left">-->
            <!--                <div class="form-group">-->
            <!--                    <input type="text" class="form-control" placeholder="Search">-->
            <!--                </div>-->
            <!--                <button type="submit" class="btn btn-default">发送</button>-->
            <!--            </form>-->

            <!-- ********  这边是右边的链接  ********** -->
            <ul class="nav navbar-nav navbar-right">

                <li class="displayLi"><a href="/shop-app/user/user-sign-up.html">注册</a></li>
                <li class="displayLi"><a href="/shop-app/user/user-sign-in.html">未登录</a></li>
                <!-- ******** 和注册登录互斥隐藏 ********  -->
                <li class="dropdown displayDropdown" style="display: none">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">
                        <label id="userNameLabel">

                        </label>
                        <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="/shop-app/user/user-info.html">个人信息</a></li>
                        <li><a href="/shop-app/address/address.html">地址管理</a></li>
                        <li><a href="/shop-app/orders/order.html">订单管理</a></li>
                        <li><a href="javascript:void(0);" onclick="exitUser()">退出</a></li>
                        <!--                        <li><a href="#">Something else here</a></li>-->
                        <!--                        <li role="separator" class="divider"></li>-->
                        <!--                        <li><a href="#">Separated link</a></li>-->
                    </ul>
                </li>
                <li><a href="/shop-app/cart/cart.html">购物车</a></li>

            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->

</nav>

</div>
<div class="row" id="div_type_list">
</div>

<!--渲染模板-->
<template id="template_type">
    <div class="col-xs-3">
      <div class="thumbnail">
            <input type="hidden" value="{tId}">
            <div class="caption">
                <h3><a href="#"><small>{tName}</small></a></h3>
            </div>
        </div>

    </div>
</template>

<script>
    //加载页面->显示商品列表
    $(function () {
        loadTypeList();
    });

    //ajax 请求后台获取商品类别列表数据
    function loadTypeList() {
        let url = "type/findAll";
        httpGetNoParams(url, function (data) {
            //业务处理
            renderingTypeList(data);
        })
    }


    //渲染显示商品类别列表
    function renderingTypeList(typeList) {
        let templateTypeHtml = $("#template_type").html();
        let typeListHtml = "";
        let tempTypeHtml;
        //遍历商品列表
        $.each(typeList, function (index, type) {
            console.log(type);
            //后台数据填充到模板中

            tempTypeHtml = templateTypeHtml.replace(/{tName}/g, type.tName);

            console.log(tempTypeHtml);
            typeListHtml += tempTypeHtml;
        });

        //拼接商品展示html
        //html 填充到页面当中
        $("#div_type_list").html(typeListHtml);
    }
</script>

<!-- ********* 徐凯航  ************* -->
<script>
    $(function () {
        //等待页面加载完成，导入用户info
        //先获取本地的
        let loginUser = getLocalData("loginUser");
        if (loginUser == null) {
            loginUser = getSessionData("loginUser");
        } else {
            //本地有值：自动登录，请求后端存入后端的session中
            // console.log(loginUser);

            //ajax 请求后台将user存到session中
            let url = "/user/putUserToSession";
            let params = {
                uName: loginUser.uName,
                uPassword: loginUser.uPassword,
            };
            httpPostApi(url, params, function (data) {
                console.log(data);
            });

        }

        // console.log("用户info:"+loginUser+"，类型为："+(typeof loginUser));
        // console.log(loginUser);

        if (loginUser == null) {
            $("#userNameLabel").html("未登录");
            //将用户登录和注册显示
            $('.displayLi').show();
            $('.displayDropdown').hide();
        } else {
            $("#userNameLabel").html(loginUser.uName);
            $('.displayDropdown').show();
            //将用户登录和注册隐藏
            $('.displayLi').hide();
        }

    });

    //退出按钮
    function exitUser() {
        if (getSessionData("loginUser") != null) {
            removeSessionData("loginUser");
        } else {
            removeLocalData("loginUser");
        }

        window.location.href = "/shop-app/index.html";
    }
</script>

</body>
</html>